<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/thor-toolkit" prefix="tt" %>
<!DOCTYPE html>
<html>
<head>
	<tt:env bundle="message"/>
	<tt:service service="myService"/>
    <title>对标管理平台</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <%@include file="../common-include.jspf"%>
    <style>
        .link1{
            color:#555;
            cursor:pointer;
            text-decoration: underline;
        }
        .link1:hover{
            color:#007ded;
        }
        .tui-grid-line .link1{
            color:#555;
        }
        .tui-grid .tui-grid-line-selected .tui-grid-cell .tui-grid-cell-content .link1{
            color:#555;
        }
        .tui-grid-line-selected .tui-grid-cell .tui-grid-cell-content .link1{
            color:#fff;
        }
    </style>
    
   
    
    <script>
    var listForm;
        $(function(){
            tui.ctrl.button("resourcManage").checked(true);
            
			var grid = tui.ctrl.grid("fileGrid");
            grid.columns([
                              { name: '文件名称', key: 'name' },
                              { name: '上传人', key: 'username'},
                              { name: '发布时间', key: 'upload_time' ,format:function(data){
                            	 if(data.rowIndex<0){return;}
                            	 data.cell.firstChild.innerHTML ="" ;
                            	 data.cell.firstChild.innerHTML =data.value.substr(0,19);
                            	 
                             }}
                          ]);
            var gridMenu=[
                          {key: 'down', value: '下载'},
                          {key: 'contract', value: '查看联系人'}
                      ];
            grid.on("rowcontextmenu",function(data){
           	  	var m=tui.ctrl.menu(gridMenu);
           	  	var item=grid.activeItem();
               	m.show({x: data.event.clientX, y: data.event.clientY}, "rT");
               	m.on("select",function(data){
               		var checked=data.item.key;
               		if(checked==="down"){
               			downloadResource(item.id);
               		}
               		if(checked==="contract"){
               			getUserInfo(item.user_id);
               		}
 					if(checked==="del"){
 						delResource(item.id,item.section_id);
               		}
               	});
               	return tui.cancelDefault(data.event);
              });
            
            grid.on("rowdblclick",function(data){
             	var item=grid.activeItem();
             	editResource(item);
             });
            
            listForm=tui.ctrl.form("resourceListForm");
            listForm.on("receive",function(data){
            	if(data.length){
            		grid.data(data);
            	}else{
            		grid.data([]);
            	}
            });
            function downloadResource(fileId){
				window.location.href = "${root}/api/download.do?fileId="+fileId;
            }
          
            function delResource(id){
            	var fm=tui.ctrl.form("delResourceForm");    
				fm.action("${root}/action/deleteResource");
				fm.immediateValue({id:id});
				fm.submitForm("resourceListForm");
				tui.askbox("确认删除?","确认删除",function(result){
            		if(result){
            			fm.submit();
            		}
            		
            	});
            }
            var fileSectionData=[];
            tui.ctrl.form("getSectionForm").on("receive",function(data){
            	var obj="{";
            	for(var i=0;i<data.length;i++){
            		fileSectionData.push({checked:false,key:data[i].key,value:data[i].value});
            		
            		if(i==data.length-1){
            			obj=obj+data[i].key;
            		}else{
            			obj=obj+data[i].key+",";
            		}
            	}
            	obj=obj+"}";
            	listForm.immediateValue({"sections":obj});
            	listForm.submit();
            	
    		});
            tui.ctrl.button("uploadResource").on("click",function(data){
            	var filesection;
            	var dlg = tui.ctrl.dialog();
            	dlg.on("open",function(){
            		filesection=tui.ctrl.input("fileSection");
            		filesection.data(fileSectionData);
            	});
            	dlg.showResource("uploadHtml","文件上传",
                   [
                      {name:"保存", func: function(){
                   	   	   var fm=tui.ctrl.form();
                      	   fm.id("addResourceForm");
                      	   fm.action("${root}/action/addResource");
                      	   var id=tui.ctrl.input("diaFileInput").value();
                      	   var arr="{";
                      	   var values=filesection.value();
                      	   for(var i=0;i<values.length;i++){
                      		   if(i<values.length-1)
                      		   	arr=arr+values[i]+",";
                      		   else
                      			arr=arr+values[i] ;
                      	   }
                      	   arr=arr+"}";
                      	   if(id){
                      		 fm.immediateValue({id:id.fileId,name:id.fileName,sectionId:arr});
                      		 fm.submit();
                      	   }else{
                      		   tui.infobox("请选择文件","选择文件");
                      	   }
                      	   
                      	   fm.submitForm("resourceListForm");
                   	  	   var proc = function() {
                              dlg.close();
                              fm.off("success", proc);
                           };
                           fm.once("success",proc);
                      }},
                      {name:"取消", func: function(){
                          dlg.close();
                      }},
                	]);
            });
            
            function getUserInfo(id){
            	var fm=tui.ctrl.form();
            	fm.id("getUserInfoForm");
            	fm.action("${root}/action/sysGetUserInfo");
            	fm.immediateValue({id:id});
            	var dlg=tui.ctrl.dialog();
            	dlg.on("open",function(){
            		fm.submit();
            		fm.on("receive",function(data){
            			tui.ctrl.input("diaCom").value(data.company);
            			tui.ctrl.input("diaName").value(data.username);
            			tui.ctrl.input("diaEmail").value(data.email);
            			tui.ctrl.input("diaPhone").value(data.phone);
            			
            			tui.ctrl.input("diaCom").readonly(true);
            			tui.ctrl.input("diaName").readonly(true);
            			tui.ctrl.input("diaEmail").readonly(true);
            			tui.ctrl.input("diaPhone").readonly(true);
            		});
            		});
            	dlg.showResource("contractHtml","联系人信息",[{name:"取消", func: function(){dlg.close();}}]);
            	}
            
            
            listForm.once("success",function(){
            	ifExist();
            });
            function ifExist(){
            	for(var i=0;i<pointsData.length;i++){
            		a=(pointsData[i]=="uploadResource");
                	if(pointsData[i]=="uploadResource"){
                   		gridMenu.push({key: 'del', value: '删除'});
                    }
                }
            }
            
        });

    </script>
    <script id="uploadHtml" type="text/html" style="">
		<div style="padding:0 10px;text-align:center;margin:0 auto">
			<span class="tui-label my-label">选择板块：</span><span id="fileSection" class="tui-input" data-type="multi-select" data-validator='{"*any":"请选择板块!"}' data-form="addResourceForm"></span><br>
	        <span class="tui-label my-label">上传文件：</span><span id="diaFileInput" class="tui-input" data-type="file" 
	            data-upload-url="${root}/api/upload.do"
	            data-placeholder='选择文件'
	            data-validator='{"*any":"请选择文件!"}' data-form="addResourceForm"></span>
        </div>
	</script>
	<!-- dialog Content -->
<script id="contractHtml" type="text/html">
	<div style="padding:0 10px">
		<span class="tui-label my-label">单位部门：</span><span id="diaCom" class="tui-input" ></span><br>
        <span class="tui-label my-label">姓名：</span><span id="diaName" class="tui-input"></span><br>
        <span class="tui-label my-label">电子邮箱：</span><span id="diaEmail" class="tui-input"></span><br>
        <span class="tui-label my-label">电话：</span><span id="diaPhone" class="tui-input"></span>
	</div>
</script>
</head>
<body style="margin:0px;background-color:#E7E9ED;">
   <%@include file="../common-header.jspf"%>
   <div class="main-body">
    <a id="uploadResource" class="tui-button tui-primary" style="float:right;display:none">上传</a>
    <div style="clear:both"></div>
    <div id="fileGrid" class="tui-grid"
      data-resizable="true"
      data-rowselectable="true"
      style="width:100%;height:550px;margin-top:10px"></div>
   </div>
</body>
<span id="delResourceForm" class="tui-form" ></span>
<span id="resourceListForm" class="tui-form" data-action="${root}/action/getResourceList"  data-method="post">
</span>
<span id="getSectionForm" class="tui-form"  data-method="get" data-auto-submit="true"
      data-action="${root}/action/getInitSection">
</span>
</html>
